package com.larissa.android.note;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class NoteListFragment extends Fragment {
    private static final String TAG="NoteListFragment";

    private NoteListViewModel mNoteListViewModel;
    private RecyclerView mNoteRecyclerView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mNoteListViewModel=new ViewModelProvider(this).get(NoteListViewModel.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater,container,savedInstanceState);
        Log.d(TAG,"onCreateView");
        View view=inflater.inflate(R.layout.fragment_note_list,container,false);
        ActionBar actionBar=((AppCompatActivity) getActivity()).getSupportActionBar();
        actionBar.setTitle(TAG);
        mNoteRecyclerView=(RecyclerView) view.findViewById(R.id.note_recycler_view);
        mNoteRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        updateUI();
        return view;
    }

//    @Override
//    public void onViewCreated(View view,Bundle savedInstanceState){
//        super.onViewCreated(view,savedInstanceState);
//        Log.d(TAG,"onViewCreated");
//        updateUI();
//    }

    private void updateUI(){
        List<Note>notes=mNoteListViewModel.mNotes;
        NoteItemAdapter adapter=new NoteItemAdapter(notes);
        mNoteRecyclerView.setAdapter(adapter);
    }

    private class NoteItemHolder extends RecyclerView.ViewHolder{
        private TextView mTitleTextView;
        private TextView mLogTextView;
        private ImageButton mImageButton;
        private Note mNote;

        public NoteItemHolder(View view){
            super(view);
            mTitleTextView=(TextView) view.findViewById(R.id.note_title);
            mLogTextView=(TextView) view.findViewById(R.id.note_log);
            mImageButton=(ImageButton) view.findViewById(R.id.note_delete);
            view.setOnClickListener(itemView -> {
                NavDirections action=NoteListFragmentDirections.showNote(mNote.getTitle(),mNote.getContent(),mNote.getNoteLog());
                Navigation.findNavController(itemView).navigate(action);
//                Bundle bundle=new Bundle();
//                ArrayList<String> noteDetails=new ArrayList<>();
//                noteDetails.add(mNote.getTitle());
//                noteDetails.add(mNote.getContent());
//                noteDetails.add(mNote.getNoteLog());
//                bundle.putStringArrayList("NoteDetail",noteDetails);
//                Navigation.findNavController(itemView).navigate(R.id.noteFragment,bundle);
            });

//            mImageButton.setOnClickListener(view1 -> Toast.makeText(getContext(),String.format("You want to delete %s",mNote.getTitle()),Toast.LENGTH_LONG).show());
        }

        public void bind(Note note){
            mNote=note;
            mTitleTextView.setText(note.getTitle());
            mLogTextView.setText(note.getNoteLog());
        }
    }

    private class NoteItemAdapter extends RecyclerView.Adapter<NoteItemHolder>{
        private List<Note> mNotes;
        public NoteItemAdapter(List<Note> notes){mNotes=notes;}

        @NonNull
        @Override
        public NoteItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            // 创建ViewHolder对象
            View view=getLayoutInflater().inflate(R.layout.note_item,parent,false);
            return new NoteItemHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull NoteItemHolder holder, int position) {
            Note note=mNotes.get(position);
            holder.bind(note);
        }

        @Override
        public int getItemCount() {
            return mNotes.size();
        }
    }
}
